package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.config.JwtUtils;
import com.ccc.demoboot.pojo.Der;
import com.ccc.demoboot.pojo.Material;
import com.ccc.demoboot.service.DerService;
import com.ccc.demoboot.service.MaterialService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author wz
 * @Date 2024/6/5 9:28
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: DerController
 */
@RequestMapping("/der")
@RestController
public class DerController extends  CommonController<DerService> {
    @Autowired
    DerService derService;

    @Autowired
    MaterialService materialService;


    /**
     * 查询所有申请信息
     *
     * @param page
     * @return
     */
    @GetMapping("/select/{page}")
    public R select(@PathVariable("page") int page) {

        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数

        List<Der> list = this.derService.list();
        PageInfo<Der> menuPageInfo = new PageInfo<>(list);
        return new R().add("pageInfo", menuPageInfo);
    }


    @PostMapping("/selectDerAll/{page}")
    public R selectDerAll(@PathVariable("page") int page){
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Map<String,Object>> list = this.derService.selectDerAll();
        PageInfo<Map<String,Object>> derPageInfo = new PageInfo<>(list);
        return new R().add("derPageInfo", derPageInfo);
    }

    /**
     * 按照条件查询数据申请信息
     *
     * @param der
     * @return
     */
    @PostMapping("/selectCondition")
    public R selectCondition(@RequestBody Der der) {
        QueryWrapper<Der> derQueryWrapper = new QueryWrapper<>();
        derQueryWrapper.setEntity(der);
        List<Der> list = this.derService.list(derQueryWrapper);
        return new R().add("list", list);
    }


    /**
     * 根据id删除申请信息
     *
     * @param der
     * @return
     */
    @PostMapping("/del")
    public R del(@RequestBody Der der) {
        QueryWrapper<Der> derQueryWrapper = new QueryWrapper<>();
        derQueryWrapper.setEntity(der);
        boolean remove = this.derService.remove(derQueryWrapper);
        return new R().add("remove", remove);
    }

    /**
     * 添加新的申请信息
     *
     * @param map
     * @return
     */
    @PostMapping("/addDer")
    public R addDer(@RequestBody Map map, @RequestHeader("jwttoken") String jwttoken) {
        Integer jwttoken1 = JwtUtils.getJwtToken(jwttoken, "id");
        Der der = new Der();
        der.setId(null);
        Integer materialid = Integer.valueOf(map.get("materialid").toString());
        Integer content = Integer.valueOf(map.get("content").toString());
        Integer status = Integer.valueOf(map.get("status").toString());
        der.setMaterialid(materialid);
        der.setContent(content);
        der.setStatus(status);
        der.setDeruserid(jwttoken1);
        boolean save = this.derService.save(der);
        return new R().add("save", save);
    }


    /**
     * 批量添加
     *
     * @param list
     * @param jwttoken
     * @return
     */
    @PostMapping("/batAddDer")
    public R batAddDer(@RequestBody List<Der> list, @RequestHeader("jwttoken") String jwttoken) {
        boolean save = this.derService.batAddDer(list);
        return new R().add("save", save);
    }

    /**
     * 查询出库申请
     *
     * @param map
     * @return
     */
    @PostMapping("/selectDer")
    public R selectDer(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        int status = Integer.valueOf(map.get("status").toString());//获取订单状态
        int page = (int) map.get("page");//获取当前页码
        int pageSize = (int) map.get("pageSize");//获取每页条数
        String codematerial = (String) map.get("codematerial");
        String materialname = (String) map.get("materialname");
        QueryWrapper<Map> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("materialname", materialname);
        queryWrapper.like("codematerial", codematerial);
        PageHelper.startPage(page, pageSize);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Map<String, Object>> derList = derService.selectAllDer(queryWrapper, status, applyuserid);
        PageInfo<Map<String, Object>> list = new PageInfo<>(derList);
        return new R().add("list", list);
    }



    /**
     * 根据id查询申请详情
     *
     * @param map
     * @return
     */
    @PostMapping("/selectOneDer")
    public R selectOneDer(@RequestBody Map<String, Object> map) {
        String batchid = (String) map.get("batchid");
        List<Map<String,Object>> one = derService.selectOne(batchid);
        return new R().add("one", one);
    }

    /**
     * uniAPP查询当前用户所有的领用
     * @param map
     * @param token
     * @return
     */
    @PostMapping("/uniSelectDer")
    public R uniSelectDer(@RequestBody Map<String, Object> map, @RequestHeader("jwttoken") String token) {
        Integer applyuserid = JwtUtils.getJwtToken(token, "id");//获取当前登录用户id
        int status = Integer.valueOf(map.get("status").toString());//获取订单状态
        String codematerial = (String) map.get("codematerial");
        String materialname = (String) map.get("materialname");
        QueryWrapper<Map> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("materialname", materialname);
        queryWrapper.like("codematerial", codematerial);
        List<Map<String, Object>> list = derService.selectAllDer(queryWrapper, status, applyuserid);
        return new R().add("list", list);
    }

    /**
     * uniAPP根据id查询领用详情
     * @param map
     * @return
     */
    @PostMapping("/uniSelectOneDer")
    public R uniSelectOneDer(@RequestBody Map<String, Object> map) {
        String batchid = (String) map.get("batchid");
        List<Map<String,Object>> one = derService.selectOne(batchid);
        return new R().add("one", one);
    }

    /**
     * uniAPP添加物资领用申请
     * @param map
     * @param jwttoken
     * @return
     */
    @PostMapping("/uniAddDer")
    public R uniAddDer(@RequestBody Map<String,Object> map, @RequestHeader("jwttoken") String jwttoken) {
        Integer deruserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        boolean save = this.derService.uniAddDer(map,deruserid);
        return new R().add("save", save);
    }

    /**
     * 修改订单状态，0不通过 1通过 3取消
     *
     * @param mapOne
     * @param jwttoken
     * @return
     */
    @PostMapping("/upDer")
    public R upDer(@RequestBody Map<String, Object> mapOne, @RequestHeader("jwttoken") String jwttoken) {
        Integer applyuserid = JwtUtils.getJwtToken(jwttoken, "id");//获取当前登录用户id
        List<Map<String,Object>> list = (List) mapOne.get("map");
        int status = Integer.valueOf(mapOne.get("status").toString());//获取订单状态
        for (int i=0;i<list.size();i++) {
            Map<String ,Object> map=list.get(i);
            String batchid = (String) map.get("batchid");
            int id = Integer.valueOf(map.get("id").toString());//获取要修改的订单id
            int content = Integer.valueOf(map.get("content").toString());
            Der der = new Der();
            der.setId(id);
            der.setStatus(status);
            der.setThroughtime(new Date());
            boolean b;
            if (status != 3) {
                der.setReviewerid(applyuserid);
                QueryWrapper<Material> wrapper = new QueryWrapper<>();
                int materialid = derService.getById(id).getMaterialid();
                Material materi = new Material();
                materi.setMaterialid(materialid);
                wrapper.setEntity(materi);
                Material material = materialService.getOne(wrapper);
                System.out.println(content+"\t"+material.getQuantity());
                if (content > material.getQuantity()) {
                    //库存数量不足以出库
                    return new R().setCode(506).setMessage("库存数量不足以出库");
                }
                b = derService.updateById(der);
                if (status == 1 && b == true) {
                    material.setQuantity(material.getQuantity() - content);
                    materialService.updateById(material);
                }
            } else {
                //取消成功
                derService.updateById(der);
                return new R().setCode(400).setMessage("取消成功");
            }
        }
        //出库成功
        return new R().setCode(502).setMessage("出库成功");

    }
}
